/*
@auther fenglei
*/
package aliyun_sls

import (
	"github.com/alibabacloud-go/tea/tea"
	sls "github.com/aliyun/aliyun-log-go-sdk"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogo/protobuf/proto"
	"time"
)

type SlsDao struct {
	sls      sls.ClientInterface
	project  string
	logStore string
	ClientIp string
}

func NewSlsDao(sls sls.ClientInterface, project, logStore string) *SlsDao {
	return &SlsDao{sls: sls, project: project, logStore: logStore}
}
func (e *SlsDao) GetLogV3(beginTime, endTime int64, query string) ([]map[string]string, error) {
	req := &sls.GetLogRequest{}
	req.PowerSQL = true
	req.From = beginTime
	req.To = endTime
	//req.Query = url.QueryEscape(query)
	req.Query = query
	gg, err := e.sls.GetLogsV3(e.project, e.logStore, req)
	if err != nil {
		return nil, err
	}
	return gg.Logs, nil
}

// 写入服务
func (e *SlsDao) Put(message *gmap.StrAnyMap) error {
	logs := []*sls.Log{}
	logArray := []*sls.LogContent{}
	message.Iterator(func(k string, v interface{}) bool {
		logArray = append(logArray, &sls.LogContent{Key: tea.String(gconv.String(k)), Value: tea.String(gconv.String(v))})
		return true
	})
	message.Clear()

	log := &sls.Log{
		Time:     proto.Uint32(uint32(time.Now().Unix())),
		Contents: logArray,
	}
	logs = append(logs, log)
	if e.ClientIp == "" {
		e.ClientIp = "127.0.0.1"
	}
	logGroup := &sls.LogGroup{
		Topic:  proto.String(""),
		Source: proto.String(e.ClientIp),
		Logs:   logs,
	}
	return e.sls.PutLogs(e.project, e.logStore, logGroup)
}
func (e *SlsDao) PutHashKey(message *gmap.StrAnyMap, hashKey string) error {
	logs := []*sls.Log{}
	logArray := []*sls.LogContent{}
	message.Iterator(func(k string, v interface{}) bool {
		logArray = append(logArray, &sls.LogContent{Key: tea.String(gconv.String(k)), Value: tea.String(gconv.String(v))})
		return true
	})
	message.Clear()

	log := &sls.Log{
		Time:     proto.Uint32(uint32(time.Now().Unix())),
		Contents: logArray,
	}
	logs = append(logs, log)
	if e.ClientIp == "" {
		e.ClientIp = "127.0.0.1"
	}
	logGroup := &sls.LogGroup{
		Topic:  proto.String(""),
		Source: proto.String(e.ClientIp),
		Logs:   logs,
	}
	return e.sls.PostLogStoreLogs(e.project, e.logStore, logGroup, tea.String(hashKey))
}

// 批量写入
func (e *SlsDao) PutArray(array *garray.Array) error {
	logs := []*sls.Log{}
	array.Iterator(func(k int, v interface{}) bool {
		content := v.(map[string]string)
		logArray := []*sls.LogContent{}
		for key, val := range content {
			logArray = append(logArray, &sls.LogContent{Key: tea.String(gconv.String(key)), Value: tea.String(gconv.String(val))})
		}
		log := &sls.Log{
			Time:     proto.Uint32(uint32(time.Now().Unix())),
			Contents: logArray,
		}
		logs = append(logs, log)
		return true
	})
	if e.ClientIp == "" {
		e.ClientIp = "127.0.0.1"
	}
	logGroup := &sls.LogGroup{
		Topic:  proto.String(""),
		Source: proto.String(e.ClientIp),
		Logs:   logs,
	}
	return e.sls.PutLogs(e.project, e.logStore, logGroup)
}
